using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SudokuHelper.Model.Values;

namespace SudokuHelper.Model {
    public static class SudokuDeskExtensions {
        /// <summary>
        /// Iterates all over the region containing cell with specified coordinates
        /// </summary>
        /// <param name="desk">Desk extension</param>
        /// <param name="row">Cell row index</param>
        /// <param name="col">Cell column index</param>
        /// <returns></returns>
        public static IEnumerable<IValue> RegionByCell(this ISudokuDesk desk, int row, int col) {
            int rgnRow = (row/3)*3,
                rgnCol = (col/3)*3;

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    yield return desk[rgnRow + i, rgnCol + j];
                }
            }
        }

        public static void ForeachInRegion(this ISudokuDesk desk, int row, int col, Action<int,int> action) {
            int rgnRow = (row / 3) * 3,
                rgnCol = (col / 3) * 3;

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    action(rgnRow + i, rgnCol + j);
                }
            }
        }

        public static void ForeachInArea(this ISudokuDesk desk, int row, int col, Action<int,int> action) {
            for (int i = 0; i < 9; i++) {
                if (col != i && desk[row, i] != null) {
                    action(row, i);
                }
                if (row != i && desk[i, col] != null) {
                    action(i, col);
                }
            }

            int rgnRow = (row/3)*3,
                rgnCol = (col/3)*3;

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (rgnRow + i != row && rgnCol + j != col)
                        action(rgnRow + i, rgnCol + j);
                }
            }
        }
    }
}
